<?php
/**
*
* @package phpBB3
* @version $Id: tags.php,v 1.0 2007/12/03 22:30:06 mikov Exp $
* @copyright (c) 2005 phpbbchina.com
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/tags');

// Grab data
$tag_id	= request_var('tag', 0);
$forum_id	= request_var('f', 0);
$start	= request_var('start', 0);
$mode	= request_var('m', 'hotest');

if ($tag_id == 0){
	// Display the tags
	$template_html = 'tags_viewtags.html';

	if ($forum_id == 0){
		$sql = 'SELECT COUNT(tag_id) AS total_tags
			FROM ' . TAG_WORDLIST_TABLE . ' WHERE tag_banned = 0';
	} else {
		$sql = 'SELECT COUNT(wl.tag_id) AS total_tags
			FROM ' . TAG_WORDLIST_TABLE . ' wl JOIN '. TAG_WORDMATCH_TABLE . ' wm 
			ON wl.tag_banned = 0 AND wl.tag_id = wm.tag_id AND wm.forum_id = ' . $forum_id;
	}

	$result = $db->sql_query($sql);
	$total_tags = (int) $db->sql_fetchfield('total_tags');
	$db->sql_freeresult($result);
	
	
	$sql = 'SELECT DISTINCT wl.tag_id, wl.tag_text, wl.tag_count ';

	switch ($mode)
	{
		case 'latest':
			// Display a listing of tags order by the time
			//include($phpbb_root_path . 'includes/functions_tags.' . $phpEx);

			$page_title = $user->lang['THE_LATEST_TAGS'];

			if ($forum_id == 0){
				$sql .= 'FROM ' . TAG_WORDLIST_TABLE . ' wl WHERE wl.tag_banned = 0 ORDER BY wl.tag_id DESC';
			} else {
				$sql .= 'FROM ' . TAG_WORDLIST_TABLE . ' wl JOIN '. TAG_WORDMATCH_TABLE . ' wm 
				ON wl.tag_banned = 0 AND wl.tag_id = wm.tag_id AND wm.forum_id = ' . $forum_id . ' 
				ORDER BY wl.tag_id DESC';
			}
		break;

		case 'hotest':
		default:

			$page_title = $user->lang['THE_HOTEST_TAGS'];
			
			if ($forum_id == 0){
				$sql .= 'FROM ' . TAG_WORDLIST_TABLE . ' wl WHERE tag_banned = 0 ORDER BY tag_count DESC';
			} else {
				$sql .= 'FROM ' . TAG_WORDLIST_TABLE . ' wl JOIN '. TAG_WORDMATCH_TABLE . ' wm 
				ON wl.tag_banned = 0 AND wl.tag_id = wm.tag_id AND wm.forum_id = ' . $forum_id . ' 
				ORDER BY tag_count DESC';
			}
	}

	$tags_per_page = $config['topics_per_page'] * 3;
	$result = $db->sql_query_limit($sql, $tags_per_page, $start);

	while ($row = $db->sql_fetchrow($result))
	{
		
		$template->assign_block_vars('tagrow', array(
			'TAG_ID'		=> $row['tag_id'],
			'TAG_TEXT'		=> $row['tag_text'],
			'U_TAG_TOPICS'	=> $phpbb_root_path . 'tags.' . $phpEx . '?tag=' . $row['tag_id'],
			'TAG_COUNT'	=> $row['tag_count'],
		));
	}
	$db->sql_freeresult($result);

	$params[] = "m=$mode";
	$params[] = "f=$forum_id";
	$pagination_url = append_sid("{$phpbb_root_path}tags.$phpEx", implode('&amp;', $params));

	// Generate page
	$template->assign_vars(array(
		'PAGINATION'	=> generate_pagination($pagination_url, $total_tags, $tags_per_page, $start),
		'PAGE_NUMBER'	=> on_page($total_tags, $tags_per_page, $start),
		'TOTAL_TAGS'	=> ($total_tags == 1) ? $user->lang['LIST_TAG'] : sprintf($user->lang['LIST_TAGS'], $total_tags))
	);
} else {
	// Display the topic list
	$template_html = 'tags_viewtopics.html';
	
	// Is banned?
	$sql = 'SELECT tag_banned, tag_text
		FROM ' . TAG_WORDLIST_TABLE . ' WHERE tag_id=' . $tag_id;
	$result = $db->sql_query($sql);
	if (!$row = $db->sql_fetchrow($result)){
		// do error message
	}
	$db->sql_freeresult($result);

	if ($row['tag_banned'] != 0){
		// do error message
	} else {
		$tag_text = $row['tag_text'];
	
		if ($forum_id == 0){
			$sql_count = 'SELECT COUNT(topic_id) AS topics_count
				FROM ' . TAG_WORDMATCH_TABLE . ' WHERE tag_id=' . $tag_id;
			$sql = 'SELECT t.*, f.forum_name 
				FROM ' . TAG_WORDMATCH_TABLE . ' wm JOIN ' . TOPICS_TABLE . ' t JOIN ' . FORUMS_TABLE . ' f ON wm.forum_id=f.forum_id AND wm.topic_id=t.topic_id AND wm.tag_id=' . $tag_id;
			
		} else {
			$sql_count = 'SELECT COUNT(wl.tag_id) AS topics_count
				FROM ' . TAG_WORDLIST_TABLE . ' wl JOIN '. TAG_WORDMATCH_TABLE . ' wm 
				ON wl.tag_id = wm.tag_id AND wm.forum_id = ' . $forum_id . ' AND wm.tag_id=' . $tag_id;
				
			$sql = 'SELECT t.*, f.forum_name 
				FROM ' . TAG_WORDLIST_TABLE . ' wl JOIN '. TAG_WORDMATCH_TABLE . ' wm JOIN ' . TOPICS_TABLE . ' t JOIN ' . FORUMS_TABLE . ' f 
				ON wm.forum_id=f.forum_id AND wl.tag_id = wm.tag_id AND wm.topic_id=t.topic_id AND wm.forum_id = ' . $forum_id . ' AND wm.tag_id=' . $tag_id;
		}

		$result = $db->sql_query($sql_count);
		$topics_count = (int) $db->sql_fetchfield('topics_count');
		$db->sql_freeresult($result);

		// Make sure $start is set to the last page if it exceeds the amount
		if ($start < 0 || $start > $topics_count)
		{
			$start = ($start < 0) ? 0 : floor(($topics_count - 1) / $config['topics_per_page']) * $config['topics_per_page'];
		}
		$sql_limit = $config['topics_per_page'];
		$result = $db->sql_query_limit($sql, $sql_limit, $start);
		
		$forum_name = '';
		while ($row = $db->sql_fetchrow($result)){
			
			// Replies
			$replies = ($auth->acl_get('m_approve', $row['forum_id'])) ? $row['topic_replies_real'] : $row['topic_replies'];
			$topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $row['forum_id'])) ? true : false;
			$posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $row['forum_id'])) ? true : false;
			
			if ($row['topic_status'] == ITEM_MOVED)
			{
				$row['topic_id'] = $row['topic_moved_id'];
				$unread_topic = false;
			}
			else
			{
				$unread_topic = false;
			}
			
			$forum_name = $row['forum_name'];

			// Get folder img, topic status/type related information
			$folder_img = $folder_alt = $topic_type = '';
			topic_status($row, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
			
			// Generate all the URIs ...
			$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $row['forum_id'] . '&amp;t=' . $row['topic_id']);
			// Send vars to template
			$template->assign_block_vars('topicrow', array(
				'FORUM_ID'					=> $row['forum_id'],
				'TOPIC_ID'					=> $row['topic_id'],
				'TOPIC_AUTHOR'				=> get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
				'TOPIC_AUTHOR_COLOUR'		=> get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
				'TOPIC_AUTHOR_FULL'			=> get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
				'FIRST_POST_TIME'			=> $user->format_date($row['topic_time']),
				'LAST_POST_SUBJECT'			=> censor_text($row['topic_last_post_subject']),
				'LAST_POST_TIME'			=> $user->format_date($row['topic_last_post_time']),
				'LAST_VIEW_TIME'			=> $user->format_date($row['topic_last_view_time']),
				'LAST_POST_AUTHOR'			=> get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
				'LAST_POST_AUTHOR_COLOUR'	=> get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
				'LAST_POST_AUTHOR_FULL'		=> get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
		
				'PAGINATION'		=> topic_generate_pagination($replies, $view_topic_url),
				'REPLIES'			=> $replies,
				'VIEWS'				=> $row['topic_views'],
				'TOPIC_TITLE'		=> censor_text($row['topic_title']),
				'TOPIC_TYPE'		=> $topic_type,
		
				'TOPIC_FOLDER_IMG'		=> $user->img($folder_img, $folder_alt),
				'TOPIC_FOLDER_IMG_SRC'	=> $user->img($folder_img, $folder_alt, false, '', 'src'),
				'TOPIC_FOLDER_IMG_ALT'	=> $user->lang[$folder_alt],
				'TOPIC_ICON_IMG'		=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
				'TOPIC_ICON_IMG_WIDTH'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
				'TOPIC_ICON_IMG_HEIGHT'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
				'ATTACH_ICON_IMG'		=> ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id) && $row['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
				'UNAPPROVED_IMG'		=> ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
		
				'S_TOPIC_TYPE'			=> $row['topic_type'],
				'S_USER_POSTED'			=> (isset($row['topic_posted']) && $row['topic_posted']) ? true : false,
				'S_UNREAD_TOPIC'		=> $unread_topic,
				'S_TOPIC_REPORTED'		=> (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false,
				'S_TOPIC_UNAPPROVED'	=> $topic_unapproved,
				'S_POSTS_UNAPPROVED'	=> $posts_unapproved,
				'S_HAS_POLL'			=> ($row['poll_start']) ? true : false,
				'S_POST_ANNOUNCE'		=> ($row['topic_type'] == POST_ANNOUNCE) ? true : false,
				'S_POST_GLOBAL'			=> ($row['topic_type'] == POST_GLOBAL) ? true : false,
				'S_POST_STICKY'			=> ($row['topic_type'] == POST_STICKY) ? true : false,
				'S_TOPIC_LOCKED'		=> ($row['topic_status'] == ITEM_LOCKED) ? true : false,
				'S_TOPIC_MOVED'			=> ($row['topic_status'] == ITEM_MOVED) ? true : false,
		
				'U_NEWEST_POST'			=> $view_topic_url . '&amp;view=unread#unread',
				'U_LAST_POST'			=> $view_topic_url . '&amp;p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id'],
				'U_LAST_POST_AUTHOR'	=> get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
				'U_TOPIC_AUTHOR'		=> get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
				'U_VIEW_TOPIC'			=> $view_topic_url)
			);
		}
		$db->sql_freeresult($result);
		
		if ($forum_id == 0){
			$page_title = $user->lang['TAG']. ' [' . $tag_text . ']';
		} else {
			$page_title = $forum_name . ' ' . $user->lang['TAG']. ' [' . $tag_text . ']';
			$template->assign_vars(array(
				'U_VIEW_FORUM'			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id"),
				'FORUM_NAME'			=> $forum_name
			));
		}

		$template->assign_vars(array(
			'U_VIEW_TAG'			=> append_sid("{$phpbb_root_path}tags.$phpEx", "tag=$tag_id"),
			'TAG_NAME'			=> $tag_text,
			'PAGINATION'	=> generate_pagination(append_sid("{$phpbb_root_path}tags.$phpEx", "tag=$tag_id&f=$forum_id"), $topics_count, $config['topics_per_page'], $start),
			'PAGE_NUMBER'	=> on_page($topics_count, $config['topics_per_page'], $start),
			'TOTAL_TOPICS'	=> ($topics_count == 1) ? $user->lang['VIEW_TAG_TOPIC'] : sprintf($user->lang['VIEW_TAG_TOPICS'], $topics_count))
		);
	}
}


// Output the page
page_header($page_title);

$template->set_filenames(array(
	'body' => $template_html)
);

page_footer();


?>